package servlet;

import dao.MessageDao;
import entity.Message;
import entity.MessageVo;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

/**
 * 获取我的留言
 */
@WebServlet("/GetMineMsgServlet")
public class GetMineMsgServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1. 获取请求参数
        req.setCharacterEncoding("utf-8");
        String userId = req.getParameter("userId");

        //2. 根据用户id查询留言列表
        JSONObject resData = new JSONObject();
        resData.put("code", 200);
        MessageDao messageDao = new MessageDao();
        try {
            List<MessageVo> list = messageDao.getMsgByUserId(userId);
            resData.put("data", list);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        //3.响应数据给前端调用者：
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.println(resData);
        writer.flush();
        writer.close();
    }
}
